package org.jeecg.modules.demo.publicplace.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.demo.base.entity.ZnzfEmployee;
import org.jeecg.modules.demo.base.service.IZnzfEmployeeService;
import org.jeecg.modules.demo.projectroom.entity.ZnzfRoom;
import org.jeecg.modules.demo.projectroom.service.IZnzfRoomService;
import org.jeecg.modules.demo.publicplace.entity.ZnzfPublic;
import org.jeecg.modules.demo.publicplace.entity.ZnzfMyPublicApplyParam;
import org.jeecg.modules.demo.publicplace.service.IZnzfPublicService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.system.service.ISysUserService;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 公共场所
 * @Author: jeecg-boot
 * @Date:   2022-04-11
 * @Version: V1.0
 */
@Api(tags="公共场所")
@RestController
@RequestMapping("/publicplace/znzfPublic")
@Slf4j
public class ZnzfPublicController extends JeecgController<ZnzfPublic, IZnzfPublicService> {
	@Autowired
	private IZnzfPublicService znzfPublicService;

	@Autowired
	private IZnzfRoomService znzfRoomService;

	@Autowired
	private IZnzfEmployeeService znzfEmployeeService;

	@Autowired
	private ISysUserService sysUserService;
	
	/**
	 * 分页列表查询
	 *
	 * @param znzfPublic
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "公共场所-分页列表查询")
	@ApiOperation(value="公共场所-分页列表查询", notes="公共场所-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZnzfPublic>> queryPageList(ZnzfPublic znzfPublic,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZnzfPublic> queryWrapper = QueryGenerator.initQueryWrapper(znzfPublic, req.getParameterMap());
		Page<ZnzfPublic> page = new Page<ZnzfPublic>(pageNo, pageSize);
		IPage<ZnzfPublic> pageList = znzfPublicService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param znzfPublic
	 * @return
	 */
	@AutoLog(value = "公共场所-添加")
	@ApiOperation(value="公共场所-添加", notes="公共场所-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZnzfPublic znzfPublic) {
		znzfPublicService.save(znzfPublic);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param znzfPublic
	 * @return
	 */
	@AutoLog(value = "公共场所-编辑")
	@ApiOperation(value="公共场所-编辑", notes="公共场所-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZnzfPublic znzfPublic) {
		znzfPublicService.updateById(znzfPublic);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "公共场所-通过id删除")
	@ApiOperation(value="公共场所-通过id删除", notes="公共场所-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		znzfPublicService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "公共场所-批量删除")
	@ApiOperation(value="公共场所-批量删除", notes="公共场所-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.znzfPublicService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "公共场所-通过id查询")
	@ApiOperation(value="公共场所-通过id查询", notes="公共场所-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZnzfPublic> queryById(@RequestParam(name="id",required=true) String id) {
		ZnzfPublic znzfPublic = znzfPublicService.getById(id);
		if(znzfPublic==null) {
			return Result.error("未找到对应数据",null);
		}
		return Result.OK(znzfPublic);
	}



    /**
    * 导出excel
    *
    * @param request
    * @param znzfPublic
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZnzfPublic znzfPublic) {
        return super.exportXls(request, znzfPublic, ZnzfPublic.class, "公共场所");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZnzfPublic.class);
    }

	/**
	 * 根据公共场所类型查询公共场所信息
	 *
	 * @return
	 */
	@ApiOperation(value="公共场所-根据公共场所类型查询公共场所信息", notes="公共场所-根据公共场所类型查询公共场所信息")
	@GetMapping(value = "/queryPublicPlaceByType")
	public Result<List<ZnzfPublic>> queryPublicPlaceByType(HttpServletRequest request, int type) {
		String uid = sysUserService.getUserId(request);
		// 根据uid找到eid
		String eid = znzfEmployeeService.findEidByUid(uid);
		if (eid.isEmpty()) {
			return Result.error("未找到对应数据",null);
		}
		String projectId = null;
		ZnzfEmployee znzfEmployee = znzfEmployeeService.findByEid(eid);
		if (znzfEmployee != null) {
			String roomId = znzfEmployee.getRoomId();
			ZnzfRoom znzfRoom = znzfRoomService.findByRoomId(roomId);
			if (znzfRoom != null) {
				projectId = znzfRoom.getProjectId();
				List<ZnzfPublic> znzfPublicList = znzfPublicService.queryPublicPlaceByType(projectId, type);
				if (znzfPublicList.size() == 0)
					return Result.error("未找到对应数据",null);
				return Result.OK(znzfPublicList);
			}
		}
		return Result.error("未找到对应数据",null);
	}

	 /**
	  * 查询用户所属项目下所有公共场所类型
	  *
	  * @return
	  */
	 //@AutoLog(value = "公共场所预约-通过id查询")
	 @ApiOperation(value="公共场所-查询用户所属项目下所有公共场所类型", notes="公共场所-查询用户所属项目下所有公共场所类型")
	 @GetMapping(value = "/queryAllPublicTypeByEid")
	 public Result<List<Integer>> queryAllPublicTypeByEid(HttpServletRequest request) {
		 String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
		 String name = JwtUtil.getUsername(token);
		 String uid = sysUserService.getUserByName(name).getId();
		 // 根据uid找到eid
		 String eid = znzfEmployeeService.findEidByUid(uid);
		 if (eid.isEmpty()) {
			 return Result.error("未找到对应数据",null);
		 }
		 String projectId = null;
		 ZnzfEmployee znzfEmployee = znzfEmployeeService.findByEid(eid);
		 if (znzfEmployee != null) {
			 String roomId = znzfEmployee.getRoomId();
			 ZnzfRoom znzfRoom = znzfRoomService.findByRoomId(roomId);
			 if (znzfRoom != null) {
				 projectId = znzfRoom.getProjectId();
			 }
		 }
		 List<Integer> publicTypeList = null;
		 if (projectId != null) {
			 publicTypeList = znzfPublicService.findAllPublicTypeById(projectId);
			 if(publicTypeList.isEmpty()) {
				 return Result.error("未找到对应数据",null);
			 }
		 }
		 return Result.OK(publicTypeList);
	 }
}
